/*
@author: tk
@file: systems.go
@time: 2022/12/2 14:52
*/
package main

import (
	"fmt"
	"strings"
)

type Userm struct {
	name string
	passwd string
	balance int
}

type ATM struct {
	bank string
	balance int
}

func (vari *Userm) saveMoney(getMoney int) {
	vari.balance += getMoney
	fmt.Println(vari.balance)

}
func (vari *ATM) saveMoney(getMoney int) {
	vari.balance += getMoney
	fmt.Println(vari.balance)
}
func (vari *Userm) subMoney(saveMoney int) {
	vari.balance -= saveMoney
	fmt.Println(vari.balance)
}
func (vari *ATM) subMoney(saveMoney int) {
	vari.balance -= saveMoney
	fmt.Println(vari.balance)
}

func main()  {
	userSpeace := make(map[string]Userm)
	atm := new(ATM)
	usert := new(Userm)
	atm.bank = "农行"
	atm.balance = 500000
	for  {
		fmt.Println(strings.Repeat("*", 25),"欢迎来到三乐购物系统",strings.Repeat("*", 25))
		fmt.Println("1.登录")
		fmt.Println("2.注册")
		fmt.Print("选择:")
		var op string
		fmt.Scan(&op)
		if op == "q" {
			break
		}else if op == "1" {

		fmt.Print("用户名:")
		var name string
		fmt.Scan(&name)
		fmt.Print("密码:")
		var passwd string
		fmt.Scan(&passwd)
			if _,ok := userSpeace[name];ok {
				if name == userSpeace[name].name && passwd == userSpeace[name].passwd{
					fmt.Println("登录成功")
			for  {
				fmt.Println(strings.Repeat("#",25),"主页面",strings.Repeat("#",25))
				fmt.Println("1.存款")
				fmt.Println("2.取款")
				fmt.Println("3.查询余额")
				fmt.Print("选择:")
				var ops string
				fmt.Scan(&ops)
						if ops == "q" {
							break
						}else if ops == "1"{
							fmt.Print("请输入存款金额:")
							var getMoney int
							fmt.Scan(&getMoney)
							usert.saveMoney(getMoney)
							atm.saveMoney(getMoney)
						}else if ops == "2" {
							fmt.Print("请输入取款金额:")
							var saveMoney int
							fmt.Scan(&saveMoney)
							if saveMoney > usert.balance {
								fmt.Println("余额不足!")
							}else {
								usert.subMoney(saveMoney)
								atm.subMoney(saveMoney)
							}

						}else if ops == "3"{
							fmt.Println("当前余额:",usert.balance,"元")
						}
					}
				}else {
					fmt.Println("用户不存在")
				}
			}
		}else if op == "2" {

			fmt.Print("用户名:")
			var reName string
			fmt.Scan(&reName)
			fmt.Print("密码:")
			var rePasswd string
			fmt.Scan(&rePasswd)
			user := Userm{reName,rePasswd,0}
			userSpeace[reName] = user
			fmt.Println(userSpeace)
		}else {
			fmt.Println("输入错误!重新输入")
		}
	}

}